package com.lovefreedom.psi.dao.impl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.lovefreedom.psi.bean.Permission;
import com.lovefreedom.psi.dao.PermissionDao;
import com.lovefreedom.psi.util.HibernateSessionFactory;

public class PermissionDaoImpl implements PermissionDao {

	@Override
	public void addPermission(Permission permission) {
		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.save(permission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void updatePermission(Permission permission) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.update(permission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public void deletePermission(Permission permission) {

		Session s = null;
		Transaction tx = null;
		try{
			s = HibernateSessionFactory.getSession();
			tx = s.beginTransaction();
			s.delete(permission);
			tx.commit();
		}finally{
			if(s!=null)
				s.close();
		}
	}

	@Override
	public Permission findPermissionById(int id) {	

		Session s = null;
		try{
			s = HibernateSessionFactory.getSession();
			Permission permission = (Permission)s.get(Permission.class,id);
			return permission;
		}finally{
			if(s!=null)
				s.close();
		}
	}
}
